#include "stdafx.h"
#include "Locker.h"

Locker::Locker()
{
	InitializeCriticalSection(&_lock);
}

Locker::~Locker()
{
	unlock();

	DeleteCriticalSection(&_lock);
}

void Locker::lock()
{
	EnterCriticalSection(&_lock);
}

void Locker::unlock()
{
	LeaveCriticalSection(&_lock);
}

bool Locker::tryLock()
{
	return TryEnterCriticalSection(&_lock) != 0;
}


AutoLocker::AutoLocker(Locker *locker)
: _locker(locker)
{
	if (_locker != NULL)
	{
		_locker->lock();
	}
}

AutoLocker::~AutoLocker()
{
	if (_locker != NULL)
	{
		_locker->unlock();
	}
}